﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections.Generic;
using Xceed.Silverlight.Data.Stats;

namespace Xceed.Silverlight.Data
{
  internal class AsyncToSyncEnumerator<T> : SyncEnumerator<T>
  {
    public AsyncToSyncEnumerator( IAsyncEnumerator<T> unsynchronizedChild )
    {
      if( unsynchronizedChild == null )
        throw new ArgumentNullException( "unsynchronizedChild" );

      if( !unsynchronizedChild.IsFullySynchronized )
        throw new ArgumentException( "The specified enumerator is not fully synchronized.", "unsynchronizedChild" );

      m_child = unsynchronizedChild;
      m_child.DataChanged += ChildDataChanged;
    }


    #region ChildEnumerator Property

    internal IAsyncEnumerator<T> ChildEnumerator
    {
      get
      {
        return m_child;
      }
    }

    #endregion ChildEnumerator Property


    public override CountResult Count( CountParameters parameters )
    {
      return m_child.SyncCount( parameters );
    }

    public override GroupCountResult GroupCount( CountParameters parameters )
    {
      return m_child.SyncGroupCount( parameters );
    }

    public override StatResults GetStatValues( StatParameters parameters )
    {
      return m_child.SyncGetStatValues( parameters );
    }

    public override bool JumpMoveAndFetch(
      JumpMoveAndFetchParameters parameters, 
      out Nullable<long> actualMoveCount, 
      out ICollection<T> fetchedItems, 
      out SourcePosition endPosition,
      out RelativeOffset endPositionOffset,
      out EstimateInt64 totalCount )
    {
      return this.m_child.SyncJumpMoveAndFetch(
               parameters,
               out actualMoveCount,
               out fetchedItems,
               out endPosition,
               out endPositionOffset,
               out totalCount );
    }

    protected virtual void ChildDataChanged( object sender, DataChangedEventArgs args )
    {
      this.RaiseDataChanged( args );
    }


    #region IDisposable Members

    protected override void Dispose( bool disposing )
    {
      base.Dispose( disposing );

      if( disposing )
      {
        m_child.DataChanged -= ChildDataChanged;
        m_child.Dispose();
      }
    }

    #endregion IDisposable Members


    #region Private Fields

    private IAsyncEnumerator<T> m_child; //null

    #endregion Private Fields
  }
}
